#include <graphics.h>
#include <math.h>
#include <stdio.h>
#include <windows.h>


void drawBaseBoard(int centerX, int centerY, int radius) // 表盘界面(比例化版本)
{
	ege_enable_aa(true);
	// 计算比例因子 (基于原始radius=200的比例)
	const float scale = radius / 200.0f;
	
	// 比例化参数
	const int sechang = static_cast<int>(20 * scale);
	const float outer_ring1 = radius + 27 * scale;
	const float outer_ring2 = radius + 22 * scale;
	const float outer_ring3 = radius + 18 * scale;
//	const float inner_ring = radius - sechang;
//	const float mid_ring = radius - sechang/2;
	
	cleardevice();
	setcolor(EGERGB(110, 110, 110));
	// 绘制一个圆
	ege_circle(centerX, centerY, radius);
	
// ==================== 第一层外环 ====================
	setcolor(EGERGB(30, 30, 30));
	setfillcolor(EGERGB(30, 30, 30));
	ege_fillpie(centerX - outer_ring1, centerY - outer_ring1, outer_ring1 * 2, outer_ring1 * 2, 0, 360, NULL);
	
	for(int i = 0; i <= 30; i++) {
		setcolor(EGERGB(20+i, 20+i, 20+i));
		setfillcolor(EGERGB(20+i, 20+i, 20+i));
		ege_fillpie(centerX - outer_ring1, centerY - outer_ring1, outer_ring1 * 2, outer_ring1 * 2, 240 + i*2, 10, NULL);
	}
	
	for(int i = 0; i <= 30; i++) {
		setcolor(EGERGB(20+i, 20+i, 20+i));
		setfillcolor(EGERGB(20+i, 20+i, 20+i));
		ege_fillpie(centerX - outer_ring1, centerY - outer_ring1, outer_ring1 * 2, outer_ring1 * 2, 370 - i*2, 10, NULL);
	}
	
// ==================== 第二层外环 ====================
	setcolor(EGERGB(10, 10, 10));
	setfillcolor(EGERGB(10, 10, 10));
	ege_fillcircle(centerX, centerY, outer_ring2);
	
	for(int i = 0; i <= 30; i++) {
		const float r1 = outer_ring2 - 2;
		setcolor(EGERGB(80+i, 80+i, 80+i));
		setfillcolor(EGERGB(80+i, 80+i, 80+i));
		ege_fillpie(centerX - r1, centerY - r1, r1 * 2, r1 * 2, 30 + i*2, 10, NULL);
	}
	
	for(int i = 0; i <= 30; i++) {
		const float r2 = outer_ring2 - 1;
		setcolor(EGERGB(80+i, 80+i, 80+i));
		setfillcolor(EGERGB(80+i, 80+i, 80+i));
		ege_fillpie(centerX - r2, centerY - r2, r2 * 2, r2 * 2, 160 - i*2, 10, NULL);
	}
	
// ==================== 第三层外环 ====================
	setcolor(EGERGB(20, 20, 20));
	setfillcolor(EGERGB(20, 20, 20));
	ege_fillpie(centerX - outer_ring3, centerY - outer_ring3, outer_ring3 * 2, outer_ring3 * 2, 0, 360, NULL);
<<<<<<< HEAD
	
	for(int i = 0; i <= 30; i++) {
		setcolor(EGERGB(20+i, 20+i, 20+i));
		setfillcolor(EGERGB(20+i, 20+i, 20+i));
		ege_fillpie(centerX - outer_ring3, centerY - outer_ring3, outer_ring3 * 2, outer_ring3 * 2, 240 + i*2, 10, NULL);
	}
	
	for(int i = 0; i <= 30; i++) {
		setcolor(EGERGB(20+i, 20+i, 20+i));
		setfillcolor(EGERGB(20+i, 20+i, 20+i));
		ege_fillpie(centerX - outer_ring3, centerY - outer_ring3, outer_ring3 * 2, outer_ring3 * 2, 370 - i*2, 10, NULL);
	}
	
//// ==================== 渐变效果环 ====================
	for(int i = 0; i <= 30; i++) 
	{
		const float cr = radius + i * 0.14f * scale;
		setcolor(EGERGB(60+i, 60+i, 60+i));
		setfillcolor(EGERGB(60+i, 60+i, 60+i));
		ege_fillpie(centerX - cr, centerY - cr, cr*2, cr*2, 240 + i*2, 10, NULL);
	}
	
	for(int i = 0; i <= 30; i++) 
	{
=======
	
	for(int i = 0; i <= 30; i++) {
		setcolor(EGERGB(20+i, 20+i, 20+i));
		setfillcolor(EGERGB(20+i, 20+i, 20+i));
		ege_fillpie(centerX - outer_ring3, centerY - outer_ring3, outer_ring3 * 2, outer_ring3 * 2, 240 + i*2, 10, NULL);
	}
	
	for(int i = 0; i <= 30; i++) {
		setcolor(EGERGB(20+i, 20+i, 20+i));
		setfillcolor(EGERGB(20+i, 20+i, 20+i));
		ege_fillpie(centerX - outer_ring3, centerY - outer_ring3, outer_ring3 * 2, outer_ring3 * 2, 370 - i*2, 10, NULL);
	}
	
// ==================== 渐变效果环 ====================
	for(int i = 0; i <= 30; i++) {
		const float cr = radius + i * 0.14f * scale;
		setcolor(EGERGB(60+i, 60+i, 60+i));
		setfillcolor(EGERGB(60+i, 60+i, 60+i));
		ege_fillpie(centerX - cr, centerY - cr, cr*2, cr*2, 240 + i*2, 10, NULL);
	}
	
	for(int i = 0; i <= 30; i++) {
>>>>>>> 4f7d5b874e40f4bb9b0a9275838d6f2d563fcd79
		const float cr = radius + i * 0.14f * scale;
		setcolor(EGERGB(60+i, 60+i, 60+i));
		setfillcolor(EGERGB(60+i, 60+i, 60+i));
		ege_fillpie(centerX - cr, centerY - cr, cr*2, cr*2, 370 - i*2, 10, NULL);
	}
	
	// 中心环（改为fillcircle）
	setcolor(EGERGB(10, 10, 10));
	setfillcolor(EGERGB(10, 10, 10));
	ege_fillcircle(centerX, centerY, radius + 6 * scale);
	
	// 最内环（改为fillcircle）
	setcolor(EGERGB(25, 25, 25));
	setfillcolor(EGERGB(25, 25, 25));
	ege_fillcircle(centerX, centerY, radius+ 4 * scale);
}

void drawRotatedText(const char* text, int x, int y, double angle,int choose,int choose2,float r) 
{
	LOGFONTW font;
	getfont(&font);
	if(choose2 == 0)
	{
<<<<<<< HEAD
		font.lfHeight = r*0.15;  // 设置字体大小
	}
	else if(choose2 == 1)
	{
		font.lfHeight = r*0.1;	
=======
		font.lfHeight = r*0.115;  // 设置字体大小
	}
	else if(choose2 == 1)
	{
		font.lfHeight = r*0.09;	
>>>>>>> 4f7d5b874e40f4bb9b0a9275838d6f2d563fcd79
	}
	font.lfEscapement = -angle * 180.0 / PI * 10;  // 设置字体旋转角度，单位为0.1度
	setfont(&font);
	if(choose == 0)
	{
		settextcolor(WHITE);  // 设置文字颜色	
	}
	else if(choose == 1)
	{
		setcolor(EGERGB(255, 255, 0));
	}
	
	setbkmode(TRANSPARENT);  // 设置背景透明
	settextjustify(CENTER_TEXT, CENTER_TEXT);  // 设置文字居中对齐	
	
	// 绘制文字
<<<<<<< HEAD
	outtextxy(x+r*0.04, y+r*0.1, text);
=======
	outtextxy(x+r*0.03, y+r*0.06, text);
>>>>>>> 4f7d5b874e40f4bb9b0a9275838d6f2d563fcd79
}

void drawDynamics(float x,float y,float r,float angle, int width, int length) 
{
	
	
	// 创建临时图像（大小要能容纳整个仪表）
	const int imgWidth = width;
	const int imgHeight = length;
	PIMAGE pimg = newimage(imgWidth, imgHeight);
	
	// 设置临时图像为当前绘图目标
	settarget(pimg);    
	// 清空临时图像（透明背景）
	setbkcolor(TRANSPARENT);
	cleardevice();
	
	// 在临时图像上绘制仪表（以图像中心为原点）
	const float centerX = imgWidth / 2.0f;    // 临时图像中心
	const float centerY = imgHeight / 2.0f;     
	
	// 启用抗锯齿绘制刻度
	ege_enable_aa(true);
<<<<<<< HEAD
	
	float scale_radius = r * 0.81;
	
=======

	float scale_radius = r * 0.81;

>>>>>>> 4f7d5b874e40f4bb9b0a9275838d6f2d563fcd79
	char num[12][3]= {"E","12","15","S","21","24","W","30","33","N","3","6"};
	setfont(r/6, 0, "Arial");
	setcolor(WHITE);
	setbkmode(TRANSPARENT);
	//绘制刻度数字字母
	for (int i = 0; i < 72; i++) 
	{ 
		setcolor(EGEARGB(255, 164, 164, 164));
		double angle = 2 * PI / 72 * i;
		int tickLength = r / 20;
		if (i % 2 == 0) 
		{
			setlinewidth(3);
			tickLength = r / 10;
		} 
		else 
		{
			setlinewidth(2);
		}
<<<<<<< HEAD
		int startX = centerX + (scale_radius - r / 20)  * cos(angle);
		int startY = centerY - (scale_radius - r / 20)  * sin(angle);
		int endX = centerX + (scale_radius- r / 20 + tickLength) * cos(angle);
		int endY = centerY - (scale_radius- r / 20 + tickLength) * sin(angle);
=======
		int startX = x + (scale_radius - r / 20)  * cos(angle);
		int startY = y - (scale_radius - r / 20)  * sin(angle);
		int endX = x + (scale_radius- r / 20 + tickLength) * cos(angle);
		int endY = y - (scale_radius- r / 20 + tickLength) * sin(angle);
>>>>>>> 4f7d5b874e40f4bb9b0a9275838d6f2d563fcd79
		ege_line(startX, startY, endX, endY);
		
		if(i % 6 == 0) 
		{
			
			int j= i / 6;
<<<<<<< HEAD
			int textX = centerX + r*0.925 * cos(angle) - textwidth(num[j]) / 2;
			int textY = centerY + r*0.925 * sin(angle) - textheight(num[j]) / 2;		
=======
			int textX = x + r*0.925 * cos(angle) - textwidth(num[j]) / 2;
			int textY = y + r*0.925 * sin(angle) - textheight(num[j]) / 2;		
>>>>>>> 4f7d5b874e40f4bb9b0a9275838d6f2d563fcd79
			drawRotatedText(num[j], textX, textY, angle+PI/2,0,0,r); 
		}
		
	}
	
	// 恢复主画布为绘图目标
	settarget(NULL); 
	
	// 旋转绘制组合图像（中心旋转）
	putimage_rotatezoom(
		NULL,           // 目标：屏幕
		pimg,           // 源图像
		width/2, length/2,      // 屏幕中心位置
		0.5f, 0.5f,     // 旋转中心比例（图像中心）
		angle, // 旋转角度
		1.0f,           // 缩放比例
		true,           // 使用透明
		-1,             // 不设置额外透明度
		true            // 平滑旋转
		);    
	
	// 释放临时图像
	delimage(pimg);
}

void drawDetails(int centerX, int centerY, int radius)
{
	//绘制黄色三角形
	int width = radius/8;  // 指针宽度	
	// 指针头部，绘制一个三角形
	setcolor(YELLOW);
	setfillcolor(YELLOW);
	int Points1[] = {centerX, centerY- radius + radius*0.25, centerX - width / 2, centerY - radius + radius*0.35, centerX + width / 2, centerY - radius + radius*0.35, centerX, centerY - radius + radius*0.25};
	fillpoly(4, Points1);
	width = radius*0.065;
	int Points2[] = {centerX, centerY+ radius - radius*0.25, centerX - width / 2, centerY + radius - radius*0.3, centerX + width / 2, centerY + radius - radius*0.3, centerX, centerY + radius - radius*0.25};
	fillpoly(4, Points2);
	
	//写字TO FR
	setcolor(WHITE);
	LOGFONTW lf1;	// 定义 LOGFONTW 结构体变量
	ZeroMemory(&lf1, sizeof(LOGFONTW));	// 清空结构体	
	// 设置字体属性
	lf1.lfHeight = radius / 10;           // 字体高度为  像素
	lf1.lfWeight = FW_BOLD;      // 字体粗细为粗体
	lf1.lfItalic = FALSE; 		// 非斜体
	lf1.lfUnderline = FALSE;     // 无下划线
	lf1.lfStrikeOut = FALSE;     // 无删除线
	lstrcpyW(lf1.lfFaceName, L"黑体"); // 字体名称为黑体	
	setfont(&lf1);	// 设置字体
	ege_outtextxy(centerX+radius*0.3,centerY-radius*0.4,"TO");
	ege_outtextxy(centerX+radius*0.3,centerY+radius*0.4,"FR");	
	
	//绘制红色矩形的字
	setcolor(EGERGB(196,23,30));
	setfillcolor(EGERGB(196,23,30));
	ege_fillrect(centerX-radius*0.42,centerY-radius*0.20,radius/10*1.5,radius/10*0.9);
	ege_fillrect(centerX+radius*0.13,centerY-radius*0.65,radius/10*0.9,radius/10*4);
	
	setcolor(BLACK);
	setfillcolor(BLACK);
	LOGFONTW lf2;	// 定义 LOGFONTW 结构体变量
	ZeroMemory(&lf2, sizeof(LOGFONTW));	// 清空结构体	
	// 设置字体属性
	lf2.lfHeight = radius / 10;           // 字体高度为  像素
	lf2.lfWeight = FW_BOLD;      // 字体粗细为粗体
	lf2.lfItalic = FALSE; 		// 非斜体
	lf2.lfUnderline = FALSE;     // 无下划线
	lf2.lfStrikeOut = FALSE;     // 无删除线
	lstrcpyW(lf2.lfFaceName, L"黑体"); // 字体名称为黑体	
	setfont(&lf2);	// 设置字体	
	ege_outtextxy(centerX-radius*0.4,centerY-radius*0.20,"CS");
	ege_outtextxy(centerX+radius*0.15,centerY-radius*0.65,"W");
	ege_outtextxy(centerX+radius*0.15,centerY-radius*0.55,"A");
	ege_outtextxy(centerX+radius*0.15,centerY-radius*0.45,"V");
	ege_outtextxy(centerX+radius*0.15,centerY-radius*0.35,"2");
	
	//	绘制中心的十字架
	setcolor(BLACK);
	setfillcolor(BLACK);
	setlinestyle(SOLID_LINE,0,2);
	ege_roundrect(centerX-radius*0.62,centerY-radius*0.08,radius*1.24,radius*0.16,2);
	ege_roundrect(centerX-radius*0.08,centerY-radius*0.62,radius*0.16,radius*1.24,2);
	
	//绘制中心圆形
	setcolor(WHITE);
	setlinestyle(SOLID_LINE,0,4);
	ege_line(centerX-radius*0.6,centerY,centerX+radius*0.6,centerY);
	ege_line(centerX,centerY-radius*0.6,centerX,centerY+radius*0.6);
	setfillcolor(EGERGB(230,230,230));
	ege_fillcircle(centerX,centerY,radius/20);
	
	//绘制多根短线
	setlinestyle(SOLID_LINE,0,2);
	for(int i = 0; i < 5; ++i)
	{
		ege_line(centerX-radius/20,centerY-radius*0.5+i*radius/10,centerX+radius/20,centerY-radius*0.5+i*radius/10);
	}
	for(int i = 0; i < 5; ++i)
	{
		ege_line(centerX-radius/20,centerY+radius*0.1+i*radius/10,centerX+radius/20,centerY+radius*0.1+i*radius/10);
	}
	for(int i = 0; i < 5; ++i)
	{
		ege_line(centerX-radius*0.5+i*radius/10,centerY-radius*0.05,centerX-radius*0.5+i*radius/10,centerY+radius*0.05);
	}
	for(int i = 0; i < 5; ++i)
	{
		ege_line(centerX+radius*0.1+i*radius/10,centerY-radius*0.05,centerX+radius*0.1+i*radius/10,centerY+radius*0.05);
	}
	
}

void drawCompass(float x, float y, float r, float angle, int width, int length)
{
	drawBaseBoard(x,y,r);		
	drawDynamics(x,y,r,angle,width,length);
	drawDetails(x,y,r);		
}
int main()
{
	int width = 800, length = 600;
	initgraph(width, length, INIT_RENDERMANUAL);
	setbkcolor(RGB(147, 147, 143));
	ege_enable_aa(true);
	setrendermode(RENDER_MANUAL);	
<<<<<<< HEAD
	int r = 150, x = 400, y = 300;
=======
	int r = 100, x = 250, y = 200;
>>>>>>> 4f7d5b874e40f4bb9b0a9275838d6f2d563fcd79
	float angle = 0;
	while (!kbhit()) 
	{
		cleardevice();
		// 绘制表盘
		drawCompass(x,y,r,angle,width,length);
		//更新增加角度
		angle +=  PI / 180;
		// 控制帧率
		delay_jfps(60);
	}	
	closegraph();
} 



